-module(svr_dist).
-behaviour(gen_server).
-export([start/2,start_link/2,init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).
-define(SERVER,?MODULE).
start(Name,Sup_Mod) ->
	gen_server:start({local,Name},?MODULE, [Sup_Mod], []).
start_link(Name,Sup_Mod) ->
	gen_server:start_link({local,Name},?MODULE,[Sup_Mod],[]).
init([Sup_Mod]) ->
	{ok,{Sup_Mod,{}}}.
handle_call({Key,Msg},_,{Sup_Mod,State}) ->
	Pid = case get(Key) of
			undefined ->
				{ok,Pid} = apply(Sup_Mod,start_child,[[Key]]),
				put(Key,Pid),
				Pid;
			Pid ->
				Pid
			end,
	gen_server:call(Pid,Msg),
	{reply,gen_server:call(Pid,Msg),{Sup_Mod,State}};
handle_call({terminate,Key},_,State) ->
	case erase(Key) of
	undefine ->
		ok;
	Pid ->
		gen_server:call(Pid,terminate)
	end,
	{reply,ok,State};
handle_call(_,_,State) ->
	{reply, {error,"no func define"}, State}.
handle_cast(_, State) ->
	{noreply,State}.
handle_info(_, State) ->
	{noreply,State}.
terminate(_, _) ->
	ok.
code_change(_, State, _) ->
	{ok,State}.
